//
//  ChangePasswordViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2019/12/5.
//  Copyright © 2019 AAIM. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField * passwordTF;
@property (nonatomic,strong) UITextField * oldPasswordTF;
@property (nonatomic,strong) UITextField * againPasswordTF;
@property (nonatomic,strong) UIButton * submitBtn;
@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"修改密码";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.oldPasswordTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.againPasswordTF];
    [self.view addSubview:self.submitBtn];
    
    [_oldPasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.view.mas_top).offset(100.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(37.f);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KHEXCOLOR(@"E0E0E0");
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.oldPasswordTF.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(0.5f);
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line.mas_bottom).offset(15.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(37.f);
    }];
    
    UIView * line2 = [UIView new];
    line2.backgroundColor = KHEXCOLOR(@"E0E0E0");
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.passwordTF.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(0.5f);
    }];
    [_againPasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line2.mas_bottom).offset(15.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(37.f);
    }];
    
    UIView * line3 = [UIView new];
    line3.backgroundColor = KHEXCOLOR(@"E0E0E0");
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.againPasswordTF.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(0.5f);
    }];
    
    [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(line3.mas_bottom).offset(30.f);
        make.height.offset(40.f);
    }];
    
    // Do any additional setup after loading the view.
}

- (void)passwardTFStrChange:(UITextView *)textFiled{
    NSLog(@"新密码");
}

- (void)oldPasswardTFStrChange:(UITextView *)textFiled{
    NSLog(@"旧密码");
}

- (void)againPasswardTFStrChange:(UITextView *)textFiled{
    NSLog(@"再一次输入密码");
}

- (void)submitActive:(UIButton*)sender{
    if ([self isEmpty:_oldPasswordTF.text]) {
        [MBProgressHUD showError:@"请输入旧密码" afterDelay:3];
        return;
    }else if ([self isEmpty:_passwordTF.text]){
        [MBProgressHUD showError:@"请输入新密码" afterDelay:3];
        return;
    }else if (_passwordTF.text.length <6){
       [MBProgressHUD showError:@"密码必须大于6位" afterDelay:3];
        return;
    }
    else if ([self isEmpty:_againPasswordTF.text]){
        [MBProgressHUD showError:@"请再次输入新密码" afterDelay:3];
        return;
    }else if(![_passwordTF.text isEqualToString:_againPasswordTF.text]) {
        [MBProgressHUD showError:@"修改失败，密码不一致" afterDelay:3];
        return;
    }
    NSLog(@"完成");
    [self requestChangePassword];
    
}

- (void)requestChangePassword{
    [RequestData changePasswordWithToken:[LoginManager shareLoginManager].userModel.token userUUid:UserFuuid withNewPwd:_passwordTF.text withOldPwd:_oldPasswordTF.text withType:@"loginPwd" Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"修改密码：%@",Dict);
        NSLog(@"修改密码----：%@",Dict[@"message"]);
        if ([Dict[@"code"] integerValue] == 200) {
            
           [self showTipHudWithText:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[LoginManager shareLoginManager] pushLoginViewController];
            });
        }
       
    } failure:^(NSString * _Nonnull errMsg) {
       [self showTipHudWithText:errMsg];
    }];
}

//限制输入框的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    if (range.length + range.location > textField.text.length) {
        return NO;
    } NSUInteger length = textField.text.length + string.length - range.length;
    
    return length <= 20;
}

//判断是否全是空格

- (BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
        
    }
    
}

- (UITextField *)oldPasswordTF{
    if (!_oldPasswordTF) {
        _oldPasswordTF = [[UITextField alloc]init];
        _oldPasswordTF.placeholder = @"旧密码";
        _oldPasswordTF.font = kMainFont(15);
        _oldPasswordTF.delegate =self;
        _oldPasswordTF.textColor = KHEXCOLOR(@"#262B2F");
        _oldPasswordTF.borderStyle = UITextBorderStyleNone;
        _oldPasswordTF.secureTextEntry = YES;
        [_oldPasswordTF addTarget:self action:@selector(oldPasswardTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _oldPasswordTF;
}

- (UITextField *)againPasswordTF{
    if (!_againPasswordTF) {
        _againPasswordTF = [[UITextField alloc]init];
        _againPasswordTF.placeholder = @"请再次输入密码";
        _againPasswordTF.font = kMainFont(15);
        _againPasswordTF.delegate =self;
        _againPasswordTF.secureTextEntry = YES;
        _againPasswordTF.textColor = KHEXCOLOR(@"#262B2F");
        _againPasswordTF.borderStyle = UITextBorderStyleNone;
        [_againPasswordTF addTarget:self action:@selector(againPasswardTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _againPasswordTF;
}

- (UITextField *)passwordTF{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"请输入新密码";
        _passwordTF.font = kMainFont(15);
        _passwordTF.delegate =self;
        _passwordTF.textColor = KHEXCOLOR(@"#262B2F");
        _passwordTF.borderStyle = UITextBorderStyleNone;
        _passwordTF.secureTextEntry = YES;
        [_passwordTF addTarget:self action:@selector(passwardTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordTF;
}

- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_submitBtn setTitleColor:KHEXCOLOR(@"ffffff") forState:UIControlStateNormal];
        [_submitBtn setBackgroundColor:KHEXCOLOR(@"#4795EF")];
        _submitBtn.layer.cornerRadius = 5;
        _submitBtn.layer.masksToBounds = YES;
        [_submitBtn addTarget:self action:@selector(submitActive:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _submitBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
